//
//  MainViewController.m
//  二维码Demo
//
//  Created by 优裁科技 on 2017/5/15.
//  Copyright © 2017年 优裁科技. All rights reserved.
//

#import "MainViewController.h"
#import <Photos/Photos.h>
#import "LCQrcodeUtil.h"

//扫码、相册识别二维码
#import "SweepViewController.h"

@interface MainViewController ()<SweepViewControllerDelegate>{
    UITextField *txtParameter;
    UIImageView *imgView;
}

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//MARK: - initView
- (void)initView {
    
    CGFloat x = 0.f;
    CGFloat y = 0.f;
    CGFloat w = 0.f;
    CGFloat h = 0.f;
    
    CGFloat manginX = 15.f;
    CGFloat manginY = 30.f;
    
    CGRect rect = [UIScreen mainScreen].bounds;
    
    //MARK:文本框
    x = manginX;
    y = 60.f;
    w = rect.size.width - 2 * manginX;
    h = 30.f;
    txtParameter = [[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
    
    txtParameter.borderStyle = UITextBorderStyleRoundedRect;
    txtParameter.placeholder = @"请输入参数 例如：username=xxxx&password=xxxx";
    txtParameter.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:txtParameter];
    
    //MARK:打开点击
    y = y + h + manginY;
    h = 40.f;
    UIButton *btnOpenDM = [UIButton buttonWithType:UIButtonTypeCustom];
    btnOpenDM.frame = CGRectMake(x, y, w, h);
    
    [btnOpenDM setTitle:@"打开点米" forState:UIControlStateNormal];
    [btnOpenDM setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnOpenDM setBackgroundColor:[UIColor blueColor]];
    
    btnOpenDM.layer.cornerRadius = 5.f;
    [btnOpenDM.layer setMasksToBounds:YES];
    
    [btnOpenDM addTarget:self action:@selector(btnOpenDMAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnOpenDM];
    
    //MARK:扫码打开
    y = y + h + manginY;
    h = 40.f;
    UIButton *btnQRCode = [UIButton buttonWithType:UIButtonTypeCustom];
    btnQRCode.frame = CGRectMake(x, y, w, h);
    
    [btnQRCode setTitle:@"点击扫码" forState:UIControlStateNormal];
    [btnQRCode setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnQRCode setBackgroundColor:[UIColor blueColor]];
    
    btnQRCode.layer.cornerRadius = 5.f;
    [btnQRCode.layer setMasksToBounds:YES];
    
    [btnQRCode addTarget:self action:@selector(btnQRCodeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnQRCode];
    
    
    //MARK:生成二维码
    y = y + h + manginY;
    h = 40.f;
    UIButton *btnCreateCode = [UIButton buttonWithType:UIButtonTypeCustom];
    btnCreateCode.frame = CGRectMake(x, y, w, h);
    
    [btnCreateCode setTitle:@"点击生成二维码" forState:UIControlStateNormal];
    [btnCreateCode setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnCreateCode setBackgroundColor:[UIColor greenColor]];
    
    btnCreateCode.layer.cornerRadius = 5.f;
    [btnCreateCode.layer setMasksToBounds:YES];
    
    [btnCreateCode addTarget:self action:@selector(btnCreateQRCodeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnCreateCode];

}


//MARK: - 点开点米
- (void) btnOpenDMAction:(UIButton *)sender {
   
    NSString *strUrl = @"Etailor://";
    NSString *strparamaters = [txtParameter.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (![strparamaters isEqualToString:@""]) {
        
        if ([strparamaters hasPrefix:strUrl]) {
            strUrl = [NSString stringWithFormat:@"%@",strparamaters];
        }
        else{
            strUrl = [NSString stringWithFormat:@"%@%@",strUrl,strparamaters];
        }
        
        strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    
    
    NSURL *url = [NSURL URLWithString:strUrl];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
    else{
        UIAlertView *alertBox = [[UIAlertView alloc] initWithTitle:@"打开失败" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alertBox show];
    }

}


//MARK: - 点击扫码
- (void) btnQRCodeAction:(UIButton *)sender{
    
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
    {
        UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"请检查设备相机是否有有授权该App访问权限" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alart show];
        return ;
    }
    
    SweepViewController *sweepViewController = [[SweepViewController alloc] init];
    sweepViewController.delegate = self;
    [self presentViewController:sweepViewController animated:YES completion:nil];
}


//MARK: - SweepViewControllerDelegate(扫码委托)
- (void)sweepViewDidFinishSuccess:(id)sweepResult{
    
    NSString *strUrl = [NSString stringWithFormat:@"%@",sweepResult];
    
    NSURL *url = [NSURL URLWithString:strUrl];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
    else{
        UIAlertView *alertBox = [[UIAlertView alloc] initWithTitle:@"打开失败" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alertBox show];
    }

}

- (void)sweepViewDidFinishError{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"扫码失败，请稍后再试" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}


//MARK: - 生成二维码
- (void)btnCreateQRCodeAction:(UIButton *)sender{

    NSString *strParam = [txtParameter.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if([strParam isEqualToString:@""]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入参数，以便生成二维码" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    
    if (imgView == nil) {
        CGRect rect = [sender frame];
        rect.size = CGSizeMake(300, 300);
        rect.origin.y += 100.f;
        rect.origin.x = ([UIScreen mainScreen].bounds.size.width - rect.size.width) / 2;
        
        imgView = [[UIImageView alloc] initWithFrame:rect];
        [self.view addSubview:imgView];
    }
    
    imgView.image = [LCQrcodeUtil createQRimageString:strParam sizeWidth:imgView.frame.size.width fillColor:[UIColor blackColor] andIsHaveLogo:YES];
    
    [self.view endEditing:YES];
}

@end
